#import "SFUEvent.h"

/**
 * An SFUEvent is an object that represents a single event at SFU.
 *
 * Conforms to the PersistentDataType protocol.
 *
 * @author      James Lin
 * @version     0.6.6
 * @since       0.0.1
 * @see         PersistentDataType
 */
@implementation SFUEvent

@synthesize eventID;
@synthesize eventHeading;
@synthesize eventLocation;
@synthesize eventStartDateTime;
@synthesize eventEndDateTime;
@synthesize eventCategory;
@synthesize eventContactInfo;
@synthesize eventDescription;
@synthesize isBookmarked;
@synthesize numKeywordsMatched;

/**
 * Initialize the SFU event with complete event details.
 *
 * @param identifier     the event identifier
 * @param heading        the title of the event
 * @param location       the location of the event
 * @param startDateTime  the starting date and time of the event
 * @param endDateTime    the ending date and time of the event
 * @param category       the type of event
 * @param contactInfo    additional event contact information
 * @param description    the description of the event
 * @param bookmarked     whether the event data is persistent
 * @return               the initialized SFUEvent
 */
- (id) initWithDetails:(int)identifier 
			   heading:(NSString*)heading 
			  location:(NSString*)location 
		 startDateTime:(NSDate*)startDateTime 
		   endDateTime:(NSDate*)endDateTime 
			  category:(NSString*)category 
		   contactInfo:(NSString*)contactInfo 
		   description:(NSString*)description 
		  isBookmarked:(BOOL)bookmarked {
	if (self = [super init]) {
		self.eventID = identifier;
		self.eventHeading = heading;
		self.eventLocation = location;
		self.eventStartDateTime = startDateTime;
		self.eventEndDateTime = endDateTime;
		self.eventCategory = category;
		self.eventContactInfo = contactInfo;
		self.eventDescription = description;
		self.isBookmarked = bookmarked;
		self.numKeywordsMatched = 0;
	}
	return self; 
} 

/**
 * Compute a unique key for the event.
 *
 * SFUEvent uses a composite key of the event id, heading, and category.
 *
 * @return  the unique key
 */
- (NSString*)getKey {
	return [NSString stringWithFormat:@"%d %@ %@", eventID, eventHeading, eventCategory];
}

/**
 * Encodes the SFUEvent for archiving options.
 *
 * Supports both keyed and non-keyed archiving.
 *
 * @param coder  the NSCoder used for encoding
 */
- (void)encodeWithCoder:(NSCoder*)coder {
	if ([coder allowsKeyedCoding]) {
		[coder encodeInt:eventID forKey:@"SFUEventID"];
		[coder encodeObject:eventHeading forKey:@"SFUEventHeading"];
		[coder encodeObject:eventLocation forKey:@"SFUEventLocation"];
		[coder encodeObject:eventStartDateTime forKey:@"SFUEventStartDateTime"];
		[coder encodeObject:eventEndDateTime forKey:@"SFUEventEndDateTime"];
		[coder encodeObject:eventCategory forKey:@"SFUEventCategory"];
		[coder encodeObject:eventContactInfo forKey:@"SFUEventContactInfo"];
		[coder encodeObject:eventDescription forKey:@"SFUEventDescription"];
		[coder encodeInt:numKeywordsMatched forKey:@"SFUEventNumKeywordsMatched"];
	} else {
		[coder encodeValueOfObjCType:@encode(int) at:&eventID];
		[coder encodeObject:eventHeading];
		[coder encodeObject:eventLocation];
		[coder encodeObject:eventStartDateTime];
		[coder encodeObject:eventEndDateTime];
		[coder encodeObject:eventCategory];
		[coder encodeObject:eventContactInfo];
		[coder encodeObject:eventDescription];
		[coder encodeValueOfObjCType:@encode(int) at:&numKeywordsMatched];
	}
}

/**
 * Initializes the SFUEvent from archive.
 *
 * Supports both keyed and non-keyed archiving.
 *
 * @param coder  the NSCoder used for decoding
 * @return       the SFUEvent decoded from archive
 */
- (id)initWithCoder:(NSCoder*)coder {
	if (!(self = [super init])) {
		return nil;
	}
	
	if ([coder allowsKeyedCoding]) {
		self.eventID = [coder decodeIntForKey:@"SFUEventID"];
		self.eventHeading = [coder decodeObjectForKey:@"SFUEventHeading"];
		self.eventLocation = [coder decodeObjectForKey:@"SFUEventLocation"];
		self.eventStartDateTime = [coder decodeObjectForKey:@"SFUEventStartDateTime"];
		self.eventEndDateTime = [coder decodeObjectForKey:@"SFUEventEndDateTime"];
		self.eventCategory = [coder decodeObjectForKey:@"SFUEventCategory"];
		self.eventContactInfo = [coder decodeObjectForKey:@"SFUEventContactInfo"];
		self.eventDescription = [coder decodeObjectForKey:@"SFUEventDescription"];
		self.numKeywordsMatched = [coder decodeIntForKey:@"SFUEventNumKeywordsMatched"];
		self.isBookmarked = YES;
	} else {
		[coder decodeValueOfObjCType:@encode(int) at:&eventID];
		self.eventHeading = [coder decodeObject];
		self.eventLocation = [coder decodeObject];
		self.eventStartDateTime = [coder decodeObject];
		self.eventEndDateTime = [coder decodeObject];
		self.eventCategory = [coder decodeObject];
		self.eventContactInfo = [coder decodeObject];
		self.eventDescription = [coder decodeObject];
		[coder decodeValueOfObjCType:@encode(int) at:&numKeywordsMatched];
		self.isBookmarked = YES;
	}
	
	return self;
}

/**
 * Deallocate the SFUEvent.
 */
- (void)dealloc {
	[eventHeading release];
	eventHeading = nil;
	[eventLocation release];
	eventLocation = nil;
	[eventStartDateTime release];
	eventStartDateTime = nil;
	[eventEndDateTime release];
	eventEndDateTime = nil;
	[eventCategory release];
	eventCategory = nil;
	[eventContactInfo release];
	eventContactInfo = nil;
	[eventDescription release];
	eventDescription = nil;
    [super dealloc];
}

@end
